package com.hsbc.dev.teamo4.sms.sm.service.impl;

import com.google.common.base.Preconditions;
import com.hsbc.dev.teamo4.sms.common.utils.data.BeanValidator;
import com.hsbc.dev.teamo4.sms.common.utils.factory.UUIDFactory;
import com.hsbc.dev.teamo4.sms.sm.dao.IPermissionDao;
import com.hsbc.dev.teamo4.sms.sm.dao.impl.PermissionDaoImpl;
import com.hsbc.dev.teamo4.sms.sm.po.Department;
import com.hsbc.dev.teamo4.sms.sm.po.Permission;
import com.hsbc.dev.teamo4.sms.sm.service.IPermissionService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author:Kevin
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.service.impl
 * @Description:
 * @Date date: 2018/7/26
 */
public class PermissionServiceImpl implements IPermissionService {
    private IPermissionDao iPermissionDao=new PermissionDaoImpl();

    @Override
    public Permission save(Permission permission){
        if(checkExist(permission.getMenuId(),permission.getPermissionName(),permission.getId())){
            try {
                throw new Exception("the same menu have same permission");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        permission.setId(UUIDFactory.getUUID());
        permission.setPermissionCode(generateCode());
        BeanValidator.validateObject(permission);
        return iPermissionDao.saveEntity(permission);
    }
    private boolean checkExist(String parentId, String departmentName, String departmentId){
        Map map=iPermissionDao.countByNameAndParentId(parentId,departmentName,departmentId);
        return map != null;

    }
    private String generateCode(){
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date())+"_"+ (int) (Math.random() * 100);
    }

    @Override
    public Permission update(Permission permission) {
        if(checkExist(permission.getMenuId(),permission.getPermissionName(),permission.getId())){
            try {
                throw new Exception("the same menu have same permission");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Permission beforePermission=iPermissionDao.queryById(permission.getId());
        Preconditions.checkNotNull(beforePermission,"update department is not exist");
        permission.setUpdateId(UUIDFactory.getUUID());
        permission.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        return iPermissionDao.updateEntity(permission);
    }

    @Override
    public Permission queryById(String id)  {
        return iPermissionDao.queryById(id);
    }

    @Override
    public List<Permission> queryEntityList(){
        return iPermissionDao.queryAllEntity();
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        return iPermissionDao.updateByIsEnable(id,bool);
    }

    @Override
    public List<Permission> queryByMenuId(String menuId) {
        return iPermissionDao.queryByMenuId(menuId);
    }
}
